package springsecurityrabclogin.controller;

import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import springsecurityrabclogin.util.LoginInfoUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

//@RestController //返回字符串或JSON
@Controller
public class UserController {

    // http://localhost:8080/
    @RequestMapping(value = "/")
//    @GetMapping
    public @ResponseBody String index(){ // @ResponseBody 注解表示方法返回字符串或json
        return "Welcome to Spring Security";
    }


    @RequestMapping(value = "/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "/welcome")
    public @ResponseBody Object welcome(Principal principal){ // @ResponseBody 注解表示方法返回字符串或json
        //拿到登录人的完整信息
        System.out.println("访问welcome接口");

        return principal;
    }

    @RequestMapping(value = "/welcome2")
    public @ResponseBody Object welcome2(Authentication authentication){ // @ResponseBody 注解表示方法返回字符串或json
        //拿到登录人的完整信息

        return authentication;
    }

    @RequestMapping(value = "/welcome3")
    public @ResponseBody Object welcome3(UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken){ // @ResponseBody 注解表示方法返回字符串或json
        //拿到登录人的完整信息

        return usernamePasswordAuthenticationToken;
    }

    @RequestMapping(value = "/welcome4")
    public @ResponseBody Object welcome4(){ // @ResponseBody 注解表示方法返回字符串或json
        //拿到登录人的完整信息

        return SecurityContextHolder.getContext().getAuthentication();
    }

    @RequestMapping(value = "/welcome5")
    public @ResponseBody Object welcome5(){ // @ResponseBody 注解表示方法返回字符串或json
        //拿到登录人的完整信息

        return LoginInfoUtil.getCurrentLoginUser();
    }

}
